﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark.Admin.Orders
{
    public partial class ViewDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Utils.CheckAuthentication())
            {
                string ID = Request.QueryString["ID"] ?? String.Empty;
                if (ID == String.Empty)
                {
                    Response.Redirect("~/Admin/Orders/Index.aspx");
                }
                else
                {
                    if (!IsPostBack)
                    {
                        LoadData(long.Parse(ID));
                    }
                }
            }
        }

        private void LoadData(long ID)
        {
            HOA_DON hoaDon = HoaDonModels.GetByID(ID);
            List<CHI_TIET_HD> chiTietHoaDon = ChiTietHDModels.GetByOrderID(ID);
            
            string status = "Chưa thanh toán";
            string payment = "Tiền mặt - Tại nhà";
            if (hoaDon.TrangThai == 1)
                status = "Đã thanh toán";
            if (hoaDon.PhuongThucThanhThoan != "COD")
                payment = "Thanh toán bằng thẻ";

            StringBuilder str = new StringBuilder();
            str.Append("<div class=\"col-lg-12\">");
            str.Append("<div class=\"panel panel-green\">");
            str.Append("<div class=\"panel-heading\">");
            str.Append("Thông Tin Khách Hàng");
            str.Append("</div>");

            str.Append("<div class=\"panel-body\">");
            str.Append("<div class=\"col-xs-12 col-sm-12 col-md-6 col-lg-6\">");
            str.Append("<div class=\"form-group\">");
            str.Append("<label>Mã đơn hàng: " + hoaDon .MaHD+ " </label>");
            str.Append("</div>");
            str.Append("<div class=\"form-group\">");
            str.Append("<label>Tên khách hàng: " + hoaDon.HoTen + " </label>");
            str.Append("</div>");
            str.Append("<div class=\"form-group\">");
            str.Append("<label>Điện thoại: " + hoaDon.DienThoai + " </label>");
            str.Append("</div>");
            str.Append("<div class=\"form-group\">");
            str.Append("<label>Email: " + hoaDon.Email + " </label>");
            str.Append("</div>");
            str.Append("<div class=\"form-group\">");
            str.Append("<label>Địa chỉ: " + hoaDon.DiaChi + " </label>");
            str.Append("</div>");
            str.Append("</div>");
            str.Append("<div class=\"col-xs-12 col-sm-12 col-md-6 col-lg-6\">");
            str.Append("<div class=\"form-group\">");
            str.Append("<label>Ghi chú: " + hoaDon.GhiChu + " </label>");
            str.Append("</div>");
            str.Append("<div class=\"form-group\">");
            str.Append("<label>Ngày đặt: " + hoaDon.NgayDat.ToString("dd/MM/yyyy") + " </label>");
            str.Append("</div>");
            str.Append("<div class=\"form-group\">");
            str.Append("<label>Phương thức thanh toán: " + payment + " </label>");
            str.Append("</div>");
            str.Append("<div class=\"form-group\">");
            str.Append("<label>Trạng thái: " + status + " </label>");
            if (hoaDon.TrangThai == 0)
            {
                string changeStatus = ResolveClientUrl("~/Admin/Orders/ChangeStatus.aspx?ID=" + hoaDon.MaHD);
                str.Append("<button title=\"Thay đổi trạng thái\" type=\"button\" style=\"margin-left:10px;margin-top:-5px;\" class=\"btn btn-primary btn-circle\" onclick=\"return changeStatusOrder('" + changeStatus + "')\" data-target=\"#modalStatusOrder\" data-toggle=\"modal\"><i class=\"fa fa-refresh\"></i></button>");
            }
            str.Append("</div>");
            str.Append("<div class=\"form-group\">");
            str.Append("<label style=\"color:red;\">Tổng tiền: " + hoaDon.ThanhTien.ToString("###,###") + " VNĐ</label>");
            str.Append("</div>");
            str.Append("</div>");
            str.Append("</div>");
            str.Append("</div>");

            str.Append("<h2>Thông Tin Chi Tiết Đơn Hàng</h2>");
            str.Append("<div class=\"table-responsive\">");
            str.Append("<table class=\"table table-bordered table-hover table-striped\">");
            str.Append("<thead>");
            str.Append("<tr>");
            str.Append("<th>Tên sản phẩm</th>");
            str.Append("<th>Số lương</th>");
            str.Append("<th>Đơn giá</th>");
            str.Append("<th>Giảm giá</th>");
            str.Append("<th>Hình ảnh</th>");
            str.Append("</tr>");
            str.Append("</thead>");
            str.Append("<tbody>");

            if (chiTietHoaDon != null && chiTietHoaDon.Count > 0)
            {
                foreach (var item in chiTietHoaDon)
                {
                    HINH_SAN_PHAM hinhSanPham = HinhSanPhamModels.GetTop1ByIDProduct(item.SAN_PHAM.ID);
                    string urlImg = ResolveClientUrl("~/images/products/");
                    if (hinhSanPham != null)
                        urlImg = ResolveClientUrl("~/images/products/" + hinhSanPham.Hinh);
                    str.Append("<tr>");
                    str.Append("<td class=\"vemid\">" + item.SAN_PHAM.TenSanPham + "</td>");
                    str.Append("<td class=\"vemid\">" + item.SoLuong + "</td>");
                    str.Append("<td class=\"vemid\">" + item.DonGia.ToString("###,###") + "</td>");
                    str.Append("<td class=\"vemid\">" + item.GiamGia + "%</td>");
                    str.Append("<td><img src=\"" + urlImg + "\" width=\"70px\" height=\"70px\" /></td>");
                    str.Append("</tr>");
                }
            }
            str.Append("</tbody>");
            str.Append("</table>");
            str.Append("</div>");


            str.Append("</div>");

            orderInfo.InnerHtml = str.ToString();

        }
    }
}